package tgc.edu.yzy.custom;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Optional;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.kafka.KafkaProperties.Admin;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import tgc.edu.yzy.entity.EimAdmin;
import tgc.edu.yzy.entity.EimAdministrator;
import tgc.edu.yzy.entity.EimRegister;
import tgc.edu.yzy.entity.EimUser;
import tgc.edu.yzy.entity.Record;
import tgc.edu.yzy.repostory.EimRegisterRepository;
import tgc.edu.yzy.security.User2;
import tgc.edu.yzy.service.AdminService;
import tgc.edu.yzy.service.AdministatorService;
import tgc.edu.yzy.service.MailServiceImpl;
import tgc.edu.yzy.service.RecordService;
import tgc.edu.yzy.service.UserService;
@Controller
public class Custom {
	@Autowired
	public AdministatorService admini;
	@Autowired
	public AdminService admin;
	@Autowired
	public UserService userservice;
	@Autowired
	public RecordService recordservice;
	@Autowired
	public EimRegisterRepository eim;
	@Autowired
	private MailServiceImpl ser;
	public void record(HttpServletRequest request,String operation,String content) {
		//获取登录对象
		SecurityContext ctx = SecurityContextHolder.getContext();
	    Authentication auth = ctx.getAuthentication();
	    User2 user = (User2) auth.getPrincipal();
	    
	    Record record=new Record();
	    String ip = request.getRemoteAddr();
	    Date date=new Date();
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    record.setTime(sdf.format(date));
	    record.setIp(ip);
	    record.setContent(content);
	    record.setName(user.getName());
	    record.setOperation(operation);
	    recordservice.save(record);
	    
	}
	
	//登入
	public void startrecord(HttpServletRequest request) {
		SecurityContext ctx = SecurityContextHolder.getContext();
	    Authentication auth = ctx.getAuthentication();
	    User2 user = (User2) auth.getPrincipal();
	    String operation="登录";
	    String content="";
	    String ip = request.getRemoteAddr();
	    Date date=new Date();
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    
	    EimAdmin a1 = admin.findById(user.getId());
	    EimAdministrator a2 = admini.findById(user.getId());
	    EimUser a3 = userservice.findById(user.getId());
	    if(a1!=null) {
	    	content="*租户管理员登录";
	    }else if(a2!=null) {
	    	content="*系统管理员登录";
	    }else{
	    	content=a3.getEimadmins().getZhmc()+"租户的用户登录";
	    }
	    Record record=new Record();
	    record.setTime(sdf.format(date));
	    record.setIp(ip);
	    record.setContent(content);
	    record.setName(user.getName());
	    record.setOperation(operation);
	    recordservice.save(record);
		System.out.println("用户登录啦");
	}
	//登出
	public void endrecord(HttpServletRequest request) {
		SecurityContext ctx = SecurityContextHolder.getContext();
	    Authentication auth = ctx.getAuthentication();
	    User2 user = (User2) auth.getPrincipal();
	    String operation="登出";
	    String content="";
	    String ip = request.getRemoteAddr();
	    Date date=new Date();
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    
	    EimAdmin a1 = admin.findById(user.getId());
	    EimAdministrator a2 = admini.findById(user.getId());
	    EimUser a3 = userservice.findById(user.getId());
	    if(a1!=null) {
	    	content="*租户管理员安全退出";
	    }else if(a2!=null) {
	    	content="*系统管理员安全退出";
	    }else{
	    	content=a3.getEimadmins().getZhmc()+"租户的用户安全退出";
	    }
	    Record record=new Record();
	    record.setTime(sdf.format(date));
	    record.setIp(ip);
	    record.setContent(content);
	    record.setName(user.getName());
	    record.setOperation(operation);
	    recordservice.save(record);
	}
	//返回登陆人ID
	public Integer getUser2() {
		SecurityContext ctx = SecurityContextHolder.getContext();
	    Authentication auth = ctx.getAuthentication();
	    User2 user = (User2) auth.getPrincipal();
		return user.getId();
	}
	public String getUserpassword() {
		SecurityContext ctx = SecurityContextHolder.getContext();
	    Authentication auth = ctx.getAuthentication();
	    User2 user = (User2) auth.getPrincipal();
	    Optional<EimRegister> optional = eim.findById(user.getId());
	    if(optional.isPresent()) {
			optional.get();
		}
		return optional.get().getPassword();
	}

	public void securitycode(String emil,HttpSession session) {
		String a=integers();
		session.setAttribute("yzm", a);
		ser.sendMail(emil, "租户管理员注册", "验证码为:"+a+"\r\n您正在注册Eim租户管理员管理员！请在短时间内输入验证码进行注册。如非本人操作，请忽略本短信。");
		
	}

	private String integers() {
		String sources = "0123456789"; // 加上一些字母，就可以生成pc站的验证码了	
		Random rand = new Random();	
		StringBuffer flag = new StringBuffer();	
		for (int j = 0; j < 6; j++) {		
			
			flag.append(sources.charAt(rand.nextInt(9)) + "");
		}	
		System.out.println(flag.toString());
		return flag.toString();
	}

	public void recorduuid(HttpServletRequest request, String operation, String content,String name) {
		//获取登录对象
				
			    Record record=new Record();
			    String ip = request.getRemoteAddr();
			    Date date=new Date();
			    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			    record.setTime(sdf.format(date));
			    record.setIp(ip);
			    record.setContent(content);
			    record.setName(name);
			    record.setOperation(operation);
			    recordservice.save(record);
		
	}
	
	
}
